package com.hxkj.utils;

import com.integral.mall.file.po.SysFile;
import com.integral.mall.file.service.SysFileService;
import com.integral.mall.file.utils.file.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName imgUploadUtils  图片上传公用类
 * @Description TODO
 * @Author Mr.zhuzhengxin
 * @Date 2018/5/22 15:46
 * @Version 1.0
 */
@Component
public class ImgUploadUtils {
    @Value("${cms.realPath}")
    private  String Path;

    @Resource
    private SysFileService sysFileService;
    //批量上传图片方法
    public   List<String> UploadImgs(MultipartFile[] file) throws IOException {
        SysFile sysFile = new SysFile();
        List<String> fileNames = new ArrayList<>();
        if(null != file && file.length > 0){
            //遍历并保存文件
            for(MultipartFile files : file){
                //获取文件名
                String fileName = files.getOriginalFilename();
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                //进行ftp上传类
                 sysFile = sysFileService.upload(suffix, "image", "tk/" + FileUtil.getYMDPatternPath(),files);
                fileNames.add(Path+sysFile.getFilePath());
            }
        }
        return fileNames;
    }

}
